package com.solution.admin.product.controller;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.solution.admin.product.entity.ProductPropertyTemplateEntity;
import com.solution.admin.product.service.ProductPropertyTemplateService;
import com.solution.common.controller.BaseController;
import com.solution.common.page.JSONGrid;
import com.solution.common.page.PageBean;
import com.solution.common.page.PageParam;
import com.solution.common.utils.string.StringUtil;

/**
 * 商品属性模版web控制层
 * @author lilp
 * @date 2017-09-01 23:36:30   
 */
@RequestMapping("/productPropertyTemplate")
@Controller
public class ProductPropertyTemplateController extends BaseController {

	private Logger logger = LoggerFactory.getLogger(ProductPropertyTemplateController.class);

	@Autowired
	private ProductPropertyTemplateService productPropertyTemplateService;
	
	/**
	 * 跳转到选择商品模版页面
	 * @param request
	 * @return
	 * @author: solin
	 * @date: 2017年10月11日 下午3:16:24
	 */
	@RequestMapping("/toProductPropertyTemplateSelector")
	public String toProductPropertyTemplateSelector(HttpServletRequest request) {
		return "product/productPropertyTemplate_selector";
	}
	
	/**
	 * 跳转商品属性模版查询页面
	 * @param ProductPropertyTemplate 接收界面查询条件参数
	 * @param request 请求对象
	 * @return 返回对应的页面视图
	 * @author lilp
 	 * @date 2017-09-01 23:36:30
	 */
	@RequestMapping("/toListProductPropertyTemplatePage")
	public String toListProductPropertyTemplatePage(ProductPropertyTemplateEntity productPropertyTemplate, HttpServletRequest request) {
		return "product/productPropertyTemplate_list";
	}

	/**
	 * 分页查询商品属性模版列表
	 * @param productPropertyTemplate 接收界面查询条件参数
	 * @param request 请求对象
	 * @return 返回对应的页面视图
	 * @author lilp
 	 * @date 2017-09-01 23:36:30
	 */
	@RequestMapping("/listProductPropertyTemplatePage")
	@ResponseBody
	public JSONGrid listProductPropertyTemplatePage(ProductPropertyTemplateEntity productPropertyTemplate, HttpServletRequest request){
		int pageNum=Integer.parseInt(request.getParameter("page").toString());
		int numPerPage=Integer.parseInt(request.getParameter("rows").toString());
		PageParam pageParam = super.getPageParam(request);
		pageParam.setPageNum(pageNum);
		pageParam.setNumPerPage(numPerPage);
		String templateName = request.getParameter("templateName");
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("templateName", templateName);
		try {
			PageBean pageBean = productPropertyTemplateService.listProductPropertyTemplatePage(pageParam, paramMap);
			return new JSONGrid(pageBean.getRecordList(),pageBean.getTotalCount());
		} catch (Exception e) {
			handleException("分页查询商品属性模版失败", logger, e);
		}
		return new JSONGrid();
	}
	
	
	/**
	 * 跳转新增页面
	 * @return string 跳转页面路径
	 * @author lilp
 	 * @date 2017-09-01 23:36:30
	 */
	@RequestMapping("/toAddProductPropertyTemplate")
	public String toAddProductPropertyTemplate(HttpServletRequest request) {
		return "product/productPropertyTemplate_form";
	}

	/**
	 * 跳转编辑页面
	 * @return string 跳转页面路径
	 * @author lilp
 	 * @date 2017-09-01 23:36:30
	 */
	@RequestMapping("/toUpdateProductPropertyTemplate")
	public String toUpdateProductPropertyTemplate(HttpServletRequest request) {
		long id = Long.valueOf(request.getParameter("id"));
		try {
			ProductPropertyTemplateEntity productPropertyTemplate = productPropertyTemplateService.getProductPropertyTemplateById(id);
			request.setAttribute("productPropertyTemplate", productPropertyTemplate);
		} catch (Exception e) {
			handleException("跳转编辑商品属性模版页面错误", logger, e);
		}
		return "product/productPropertyTemplate_form";
	}


	/**
	 * 新增或保存商品属性模版
	 * @param request 请求对象
	 * @param productPropertyTemplateEntity 商品属性模版实体
	 * @return
	 * @author lilp
 	 * @date 2017-09-01 23:36:30
	 */
	@RequestMapping("/saveOrUpdateProductPropertyTemplate")
	public String saveProductPropertyTemplate(HttpServletRequest request, ProductPropertyTemplateEntity productPropertyTemplateEntity) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		int result = 0;
		try {
			// 商品属性ids，ids中的顺序做为sort_no值
			// String propertyIds = request.getParameter("propertyIds");
			String id = request.getParameter("id");
			if(StringUtil.isNotEmpty(id)){
				//修改，修改的思路：删除现在的属性，重新插入新的属性
				result = productPropertyTemplateService.updateProductPropertyTemplate(productPropertyTemplateEntity);
			}else{
				// 保存商品属性模板
				result = (int)productPropertyTemplateService.saveProductPropertyTemplate(productPropertyTemplateEntity);
			}
			resultMap.put("code", result);
		} catch (Exception e) {
			handleException("添加商品属性模版失败", logger, e);
		}
		return "redirect:/productSpec/toListProductSpecPage?tabIndex=3";
	}
	
	
	/**
	 * 将商品属性添加到模块中
	 * @param request 请求对象
	 * @return
	 * @author lilp
 	 * @date 2017-09-01 23:36:30
	 */
	@RequestMapping("/addProductPropertyToTemplate")
	@ResponseBody
	public Map<String, Object> addProductPropertyToTemplate(HttpServletRequest request) {
		
		Map<String, Object> resultMap = new HashMap<String, Object>();
		String propertyId = request.getParameter("propertyId");
		String templateId = request.getParameter("templateId");
		try {
			Map<String, Object> param = new HashMap<String, Object>();
			param.put("propertyId", propertyId);
			param.put("templateId", templateId);
			int result = productPropertyTemplateService.addProductPropertyToTemplate(param);
			resultMap.put("code", result);
		} catch (Exception e) {
			handleException("将商品属性添加到模块中失败", logger, e);
		}
		return resultMap;
	}
	
	
	/**
	 * 将商品属性从模板中移除
	 * @param request 请求对象
	 * @return
	 * @author lilp
 	 * @date 2017-09-01 23:36:30
	 */
	@RequestMapping("/deletePropertyFromeTemplate")
	@ResponseBody
	public Map<String, Object> deletePropertyFromeTemplate(HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		String propertyId = request.getParameter("propertyId");
		String templateId = request.getParameter("templateId");
		try {
			Map<String, Object> param = new HashMap<String, Object>();
			param.put("propertyId", propertyId);
			param.put("templateId", templateId);
			int result = productPropertyTemplateService.deleteProductPropertyFromTemplate(param);
			resultMap.put("resultCode", result>0?"1":"0");
		} catch (Exception e) {
			handleException("将商品属性从模板中移除失败", logger, e);
		}
		return resultMap;
	}
	
	
	/**
	 * 修改商品模板中商品属性的顺序 
	 * @param request 请求对象
	 * @return
	 * @author lilp
	 * @throws UnsupportedEncodingException 
 	 * @date 2017-08-29 14:31:56
	 */
	@RequestMapping("/updateProductPropertyTemplatePropertySort")
	@ResponseBody
	public Map<String, String> updateProductSpecSort(HttpServletRequest request) throws UnsupportedEncodingException {
		Map<String, String> resultMap = new HashMap<String, String>();
		request.setCharacterEncoding("UTF-8");
		String templateId = request.getParameter("templateId");
		String id = request.getParameter("id");
		String direction = request.getParameter("direction");
		int sortNo = Integer.parseInt(request.getParameter("sortNo"));
		try {
			// 修改排序值
			int result = productPropertyTemplateService.updateProductPropertyTemplatePropertySort(templateId, id, direction,sortNo);
			resultMap.put("resultCode", result>0?"1":"0");
			resultMap.put("resultMsg", result>0?"操作成功":"操作失败");
		} catch (Exception e) {
			handleException("修改商品模板中商品属性的顺序 错误", logger, e);
			resultMap.put("code", "0");
			resultMap.put("resultMsg", "操作失败");
		}
		return resultMap;
	}
	
	
	/**
	 * 删除商品属性模板
	 * @param request 请求对象
	 * @return
	 * @author lilp
 	 * @date 2017-09-01 23:36:30
	 */
	@RequestMapping("/deleteProductPropertyTemplate")
	@ResponseBody
	public Map<String, Object> deleteProductPropertyTemplate(HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		String templateId = request.getParameter("id");
		try {
			int result = productPropertyTemplateService.deleteProductPropertyTemplateById(Long.parseLong(templateId));
			resultMap.put("resultCode", result);
		} catch (Exception e) {
			handleException("删除商品属性模板失败", logger, e);
		}
		return resultMap;
	}
	
}
